// pages/goods_detail/goods_detail.js
import {
  getGoodsDetailData
} from '../../service/goods_list'
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsObj: {},
    iscollect: false,
    collect: []
  },

  //网络请求所需的参数
  QueryParams: {
    goods_id: Number
  },

  //筛选图片预览数据
  urls: [],

  //存放商品所有数据
  goodsInfo: {},


  //----------获取详情网络请求----------
  _getGoodsDetailData() {
    getGoodsDetailData({
      data: this.QueryParams
    }).then(res => {
      console.log(res)
      //获取轮播图信息
      const goodsObj = res.data.message
      this.goodsInfo = goodsObj

      //首先获取收藏缓存
      const collect = wx.getStorageSync('collect') || [];
      //判断当前商品是否被收藏
      let iscollect = collect.some(v => v.goods_id === this.goodsInfo.goods_id)

      //图片预览数据处理
      this.urls = goodsObj.pics.map(v => v.pics_mid)

      this.setData({
        goodsObj: {
          goods_price: goodsObj.goods_price,
          goods_name: goodsObj.goods_name,

          // 部分iphone不支持webp格式   最好找后台改  临时自己改，确保后台存在把webp格式改为jpg
          goods_introduce: goodsObj.goods_introduce.replace(/\.webp/g, ".jpg"),

          pics: goodsObj.pics,
        },
        iscollect
      })
    })
  },

  // onShow:function () {
  //   let pages=getCurrentPages();
  //   let currentPage=pages[pages.length-1];
  //   let options=currentPage.options
  //   console.log(options)
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1];
    let options = currentPage.options
    this.QueryParams.goods_id = options.goods_id
    this._getGoodsDetailData()


  },

  // ---------监听点击事件---------

  //监听子组件传递的事件
  delPreviewImage(e) {
    const current = e.detail
    const urls = this.urls
    wx.previewImage({
      current,
      urls,
    })
  },

  //点击加入购物车
  handleCartAdd() {
    //1.获取缓存中的购物车数据 数组格式
    const cart = wx.getStorageSync('cart') || []
    //2.判断 商品对象是否存在于购物车中，获取对应商品的索引值
    const index = cart.findIndex(v => v.goods_id === this.goodsInfo.goods_id)
    if (index === -1) {
      //3.不存在 第一次添加
      this.goodsInfo.num = 1
      //给商品添加选中状态
      this.goodsInfo.checked = true

      cart.push(this.goodsInfo)
    } else {
      // 4 . 已经存在于购物车  执行++
      cart[index].num++
    }
    // 5.把购物车重新添加到缓存中
    wx.setStorageSync('cart', cart)

    //6.弹窗提示
    wx.showToast({
      title: '加入购物车成功',
      icon: 'success',
      mask: true
    })
  },

  //点击立即购买
  handleGoodsPay(){
    const imCart=[]
    this.goodsInfo.num = 1
    imCart.push(this.goodsInfo)
    // 5.把购物车重新添加到缓存中
    wx.setStorageSync('imCart', imCart)
    wx.navigateTo({
      url: '/pages/pay/pay',
    })
  },

  //点击收藏按钮
  handleCollect() {
    let iscollect = false
    //首先获取收藏缓存
    const collect = wx.getStorageSync('collect') || []
    //判断该物品是否已被收藏
    const index = collect.findIndex(v => v.goods_id === this.goodsInfo.goods_id)
    //当index=-1被收藏过
    if (index !== -1) {
      //能找到删除该商品
      iscollect = false
      collect.splice(index, 1)
      //6.弹窗提示
      wx.showToast({
        title: '取消收藏成功',
        icon: 'success',
        mask: true
      })
    } else {
      collect.push(this.goodsInfo)
      iscollect = true
      //6.弹窗提示
      wx.showToast({
        title: '收藏成功',
        icon: 'success',
        mask: true
      })
    }
    //存入缓存
    wx.setStorageSync('collect', collect);
    this.setData({
      iscollect
    })
  },

})